package cardGame;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

import javax.swing.JOptionPane;

public class WarGame2 extends Game {
	List<Card> stakes = new ArrayList<Card>();;
	boolean playOn = true;
	int numPlayers=2;
	int battles =0;
	public static void main(String[] args) {
		new WarGame2().play();
	}

	@Override
	protected void play() {
		initPlayers();
		HoyleDeck bicycle =new HoyleDeck();
		bicycle.shuffle();
		deal(bicycle);
//		for(Player p : players) {
//			 for(Card c : p.hand){
//				 System.out.println(c.getFaceValue());
//			 }
//		}
		do {
			battle();
			battles++;
			System.out.println("battles:" + battles);
		} while(playOn);
		
	}
	private void initPlayers() {
		for (int i =0 ; i < numPlayers; i++) {		//starts at 0
			Scanner sc = new Scanner(System.in);
			String name=
					JOptionPane.showInputDialog ("Enter Player"+(i+1)+"'s name :");  //gets a name from user and assigns it to a player
			this.players.add(new Player(name));
		}
	}

	
	private void battle() { 
		
		//one of the players is out of cards
		for(Player p : this.players)
			if(p.hand.size()==0)
				gameOver();
		
		System.out.println(this.players.get(0).name +":  "+ this.players.get(0).hand.get(0).getFaceValue() + "	" + this.players.get(1).name +":  "+ this.players.get(1).hand.get(0).getFaceValue());
//		  try {
//			Thread.sleep(1000L);
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		}    // one second

		
		//compare top cards
		//if player0 wins
		if(this.players.get(0).hand.get(0).getFaceValue().ordinal() > this.players.get(1).hand.get(0).getFaceValue().ordinal()) {
			//player0 gets the card
			giveCard(players.get(0), players.get(1), players.get(1).hand.get(0));
			Collections.rotate(this.players.get(0).hand, -1);		//moves top card to bottom
			
			//gives stakes if any from previous round
			this.players.get(0).hand.addAll(stakes);		
			this.stakes.clear();		
			return;
		}
		
		//if player1 wins (same thing)
		if(this.players.get(1).hand.get(0).getFaceValue().ordinal() > this.players.get(0).hand.get(0).getFaceValue().ordinal()) {
			
			giveCard(players.get(1), players.get(0), players.get(0).hand.get(0));
			Collections.rotate(this.players.get(0).hand, -1);	//move his card played to back of hand
			
			this.players.get(1).hand.addAll(stakes);	
			this.stakes.clear();		
			return;
		}
		
		//tie
		if(this.players.get(1).hand.get(0).getFaceValue().ordinal() == this.players.get(0).hand.get(0).getFaceValue().ordinal()) {
			System.out.println("It's a tie! The stakes have been raised!");
			
			int cardsLeft = 5;		//1 card from tie, 3 for stakes, and 1 for other battle
			for (Player p : this.players) {
				if(cardsLeft>=p.hand.size())
					cardsLeft=p.hand.size();
				
				if (p.hand.size()==1){
					gameOver();
					
				}
			}
				
			//each player puts 3 cards + previous card in stakes
			for(int i=0; i<cardsLeft-1 ; i++) //4 cards normally
				for (Player p : this.players) {	//players
					stakes.add(p.hand.get(0));
					p.hand.remove(p.hand.get(0));
					return;	
			}
		}
	}
	
	private void gameOver() {
		if(players.get(0).hand.isEmpty() ) {
			System.out.println("Congratulations " + players.get(1).name  + ", you've won!");
		}
		if(players.get(1).hand.isEmpty() ) {
			System.out.println("Congratulations " + players.get(0).name  + ", you've won!");
		}

		System.exit(0);
	}

	
	private void giveCard (Player giver, Player reciever, Card cardGiven) {
		reciever.hand.add(cardGiven);
		giver.hand.remove(cardGiven);
	}
	

	private void deal(HoyleDeck deck) {
		int extra = deck.getDeck().length % numPlayers;
		int cardsPerPlayer = (deck.getDeck().length-extra) /numPlayers;
		
		for( int j = 1; j<=cardsPerPlayer; j++)
			for (Player p : this.players) {
				p.getHand().add(deck.deal());
			}
	}

	@Override
	protected int getMaxPlayers() {
		// TODO Auto-generated method stub
		return 2;
	}
	
	@Override
	protected int getMinPlayers() {
		// TODO Auto-generated method stub
		return 2;
	}
}
